import { useModel } from "umi";
import { useReactive } from "ahooks";
import { useEffect } from "react";
import compress from "gql-compress";

const usePositionMembers = () => {
    const { graphqlFetch } = useModel("global");

    const state = useReactive({
        loading: false,
        members: [],
    });

    const loadPositionMembers = async (opUuid) => {
        state.loading = true;
        let res = await graphqlFetch({
            query,
            variables: { opUuid },
        });
        state.members = res?.a?.nodes || [];
        state.loading = false;
        return res?.a?.nodes || [];
    };

    return { ...state, loadPositionMembers };
};

const query = compress(`query MyQuery($opUuid: UUID!) {
    a: allOrganizationStaffs(
        condition: { opUuid: $opUuid, osStat: true }
    ) {
        nodes {
            osUuid
            omUuid
            osRoge
            osName
            deInfo
            isBoss
            opUuid
        }
    }
}`);

export default usePositionMembers;
